<?php

 function ensure_field_was_submitted($field_name)
 {
         if (isset($_POST[$field_name]) && $_POST[$field_name] != '' && strlen($_POST[$field_name]) > 0)
                 return TRUE;
         else
                 return FALSE;
 }

 function ensure_field_is_correct_length($field_name)
 {
         if (strlen($_POST[$field_name]) > 0 && $_POST[$field_name] != '')
                 return TRUE;
         else
                 return FALSE;
 }

 function show_error($message){
     echo $message;
 }

 // 1. Collect the input from the form.
 //              Prep the input
 $_POST['fname'] = trim($_POST['fname']);
 $_POST['lname']  = trim($_POST['lname']);
 $_POST['addy'] = $_POST['addy'];
 $_POST['city'] = $_POST['city'];

 // 2. Validating the input
 //
 $required_fields = array('fname', 'lname', 'addy', 'city');

 // 2.1 Ensure that the user typed in the fields we want
 foreach($required_fields as $field)
 {
         //echo "<p>Checking to ensure $field was submitted...";
         if ( ! ensure_field_was_submitted($field)){
            $message = "You need to enter the field: $field";
            show_error($message);
            exit();
            //header("Location: submit.php");
         }
 }

 // 2.2 Ensure that some of the fields meet the length requirement
 if ( ! ensure_field_is_correct_length('fname'))
         die("The username should be less than 50 characters");
 if ( ! ensure_field_is_correct_length('lname'))
         die("The email should be less than 100 characters");
 if ( ! ensure_field_is_correct_length('lname'))
         die("The email should be less than 100 characters");
 if ( ! ensure_field_is_correct_length('lname'))
         die("The email should be less than 100 characters");




 // 3. Add to the database, or do whatever
 //              Notifty the user of what happened
 echo "<p>Name:{$_POST['fname']} {$_POST['lname']}</p>";
 echo "<p>Address: {$_POST['addy']} City: {$_POST['city']}</p>";
 echo "<p>Phone: {$_POST['phone']}</p>";
 echo "<p>Sandwich:{$_POST['sandwich_type']}</p>";

 /* EOF */